import java.util.ArrayList;
import java.util.List;

/**
 * @BelongsProject : LeetCode
 * @Author : WXY
 * @CreateTime : 2022-12-08  19:58
 * @Description :剑指 Offer 29. 顺时针打印矩阵
 * @Version : 1.0
 */

public class Num029_spiralOrder {
    public static int[] spiralOrder(int[][] matrix) {
        if (matrix.length == 0) return new int[0];
        if (matrix.length == 1) return matrix[0];
        int tR = 0;
        int tC = 0;
        int dR = matrix.length - 1;
        int dC = matrix[0].length - 1;
        List<Integer> list = new ArrayList<>();
        int[] arr = new int[(dR + 1) * (dC + 1)];
        while (tR <= dR && tC <= dC) {
            printEdge(matrix, tR++, tC++, dR--, dC--,list);
        }
        for (int i = 0; i < arr.length; i++) {
            arr[i] = list.get(i);
        }
        return arr;
    }

    public static void printEdge(int[][] m, int tR, int tC, int dR, int dC, List<Integer> list) {
        if (tR == dR) {
            for (int i = tC; i <= dC; i++) {
                list.add(m[tR][i]);
            }
        } else if (tC == dC) {
            for (int i = tR; i <= dR; i++) {
                list.add(m[i][tC]);
            }
        } else {
            int curC = tC;
            int curR = tR;
            while (curC != dC) {
                list.add(m[tR][curC]);
                curC++;
            }
            while (curR != dR) {
                list.add(m[curR][dC]);
                curR++;
            }
            while (curC != tC) {
                list.add(m[dR][curC]);
                curC--;
            }
            while (curR != tR) {
                list.add(m[curR][tC]);
                curR--;
            }
        }
    }




    public static int[] spiralOrder1(int[][] matrix) {
        if (matrix.length == 0) return new int[0];
        if (matrix.length == 1) return matrix[0];
        int tR = 0;
        int tC = 0;
        int dR = matrix.length - 1;
        int dC = matrix[0].length - 1;
        int[] arr = new int[(dR + 1) * (dC + 1)];

        while (tR <= dR && tC <= dC) {
            printEdge1(matrix, tR++, tC++, dR--, dC--, arr);
        }

        return arr;
    }

    public static int index = 0;

    public static void printEdge1(int[][] m, int tR, int tC, int dR, int dC, int[] arr) {
        if (tR == dR) {
            for (int i = tC; i <= dC; i++) {
                arr[index++] = m[tR][i];
            }
        } else if (tC == dC) {
            for (int i = tR; i <= dR; i++) {
                arr[index++] = m[i][tC];
            }
        } else {
            int curC = tC;
            int curR = tR;
            while (curC != dC) {
                arr[index++] = m[tR][curC];
                curC++;
            }
            while (curR != dR) {
                arr[index++] = m[curR][dC];
                curR++;
            }
            while (curC != tC) {
                arr[index++] = m[dR][curC];
                curC--;
            }
            while (curR != tR) {
                arr[index++] = m[curR][tC];
                curR--;
            }
        }
    }

    public static void main(String[] args) {
        int[][] matrix = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 },
                { 13, 14, 15, 16 } };
        int[] ints = spiralOrder1(matrix);
        for (int i = 0; i < ints.length; i++) {
            System.out.print(ints[i] + " ");
        }

    }


}
